/*
 * EncoladorPortletPage.java
 *
 * Created on February 15, 2008, 10:30 AM
 * Copyright fsanchez
 */
package operacionesencolador;

import com.computec.encolador.constantes.ConstantesGenerales;
import com.computec.encolador.delegate.EncoladorDelegate;
import com.computec.encolador.source.EncoladorSource;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.rave.web.ui.component.Checkbox;
import com.sun.rave.web.ui.component.CheckboxGroup;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.faces.FacesException;
import com.sun.rave.web.ui.component.Form;
import com.sun.rave.web.ui.component.ThemeLinks;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlCommandButton;
import com.sun.rave.web.ui.component.Tree;
import com.sun.rave.web.ui.component.TreeNode;
import com.sun.rave.web.ui.component.ImageComponent;
import javax.faces.component.html.HtmlSelectBooleanCheckbox;
import com.sun.rave.web.ui.component.DropDown;
import com.sun.rave.web.ui.model.SingleSelectOptionsList;
import com.sun.rave.web.ui.component.Alert;


public class EncoladorPortletPage extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Creator-managed Component Definition">
    
    private int __placeholder;
    
    private ThemeLinks themeLinks1 = new ThemeLinks();
    
    public ThemeLinks getThemeLinks1() {
	return themeLinks1;
    }
    
    public void setThemeLinks1(ThemeLinks t) {
	this.themeLinks1 = t;
    }
    
    private Form form1 = new Form();
    
    public Form getForm1() {
	return form1;
    }
    
    public void setForm1(Form f) {
	this.form1 = f;
    }

    private HtmlOutputText outputTextOp = new HtmlOutputText();

    public HtmlOutputText getOutputTextOp() {
        return outputTextOp;
    }

    public void setOutputTextOp(HtmlOutputText hot) {
        this.outputTextOp = hot;
    }

    private HtmlInputText textFieldOp = new HtmlInputText();

    public HtmlInputText getTextFieldOp() {
        return textFieldOp;
    }

    public void setTextFieldOp(HtmlInputText hit) {
        this.textFieldOp = hit;
    }

    private HtmlCommandButton buttonOp = new HtmlCommandButton();

    public HtmlCommandButton getButtonOp() {
        return buttonOp;
    }

    public void setButtonOp(HtmlCommandButton hcb) {
        this.buttonOp = hcb;
    }

    private Tree treeFile = new Tree();

    public Tree getTreeFile() {
        return treeFile;
    }

    public void setTreeFile(Tree t) {
        this.treeFile = t;
    }

    private TreeNode treeNodeFolder = new TreeNode();

    public TreeNode getTreeNodeFolder() {
        return treeNodeFolder;
    }

    public void setTreeNodeFolder(TreeNode tn) {
        this.treeNodeFolder = tn;
    }

    private ImageComponent image1 = new ImageComponent();

    public ImageComponent getImage1() {
        return image1;
    }

    public void setImage1(ImageComponent ic) {
        this.image1 = ic;
    }

    private HtmlCommandButton buttonProcesar = new HtmlCommandButton();

    public HtmlCommandButton getButtonProcesar() {
        return buttonProcesar;
    }

    public void setButtonProcesar(HtmlCommandButton hcb) {
        this.buttonProcesar = hcb;
    }

    private DropDown dropDownCategoria = new DropDown();

    public DropDown getDropDownCategoria() {
        return dropDownCategoria;
    }

    public void setDropDownCategoria(DropDown dd) {
        this.dropDownCategoria = dd;
    }

    private DropDown dropDownImpresora = new DropDown();

    public DropDown getDropDownImpresora() {
        return dropDownImpresora;
    }

    public void setDropDownImpresora(DropDown dd) {
        this.dropDownImpresora = dd;
    }

    private DropDown dropDownJob = new DropDown();

    public DropDown getDropDownJob() {
        return dropDownJob;
    }

    public void setDropDownJob(DropDown dd) {
        this.dropDownJob = dd;
    }

    private HtmlOutputText outputTextJob = new HtmlOutputText();

    public HtmlOutputText getOutputTextJob() {
        return outputTextJob;
    }

    public void setOutputTextJob(HtmlOutputText hot) {
        this.outputTextJob = hot;
    }

    private HtmlOutputText outputTextImp = new HtmlOutputText();

    public HtmlOutputText getOutputTextImp() {
        return outputTextImp;
    }

    public void setOutputTextImp(HtmlOutputText hot) {
        this.outputTextImp = hot;
    }

    private HtmlOutputText outputTextCat = new HtmlOutputText();

    public HtmlOutputText getOutputTextCat() {
        return outputTextCat;
    }

    public void setOutputTextCat(HtmlOutputText hot) {
        this.outputTextCat = hot;
    }

    private Alert alertMessage = new Alert();

    public Alert getAlertMessage() {
        return alertMessage;
    }

    public void setAlertMessage(Alert a) {
        this.alertMessage = a;
    }
    
    // </editor-fold>
    public EncoladorPortletPage() {
	// <editor-fold defaultstate="collapsed" desc="Creator-managed Component Initialization">
	try {
	} catch (Exception e) {
	    log("Page1 Initialization Failure", e);
	    throw e instanceof javax.faces.FacesException ? (FacesException) e: new FacesException(e);
	}
	// </editor-fold>
	// TODO: Add your own initialization code here (optional)
    }

    /** 
     * <p>Return a reference to the scoped data bean.</p>
     */
    protected RequestEncoladorBean getRequestEncoladorBean() {
        return (RequestEncoladorBean)getBean("RequestEncoladorBean");
    }


    /** 
     * <p>Return a reference to the scoped data bean.</p>
     */
    protected ApplicationEncoladorBean getApplicationEncoladorBean() {
        return (ApplicationEncoladorBean)getBean("ApplicationEncoladorBean");
    }


    /** 
     * <p>Return a reference to the scoped data bean.</p>
     */
    protected SessionEncoladorBean getSessionEncoladorBean() {
        return (SessionEncoladorBean)getBean("SessionEncoladorBean");
    }


    /** 
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here

        // <editor-fold defaultstate="collapsed" desc="Creator-managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("PortletPage1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here

    }

    /** 
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    public void preprocess() {
    }

    /** 
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    public void prerender() {
    }

    /** 
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    public void destroy() {
    }

    /** 
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }

    private Checkbox checkBox = null;

    public Checkbox getCheckBox() {
        return checkBox;
    }

    public void setCheckBox(Checkbox c) {
        this.checkBox = c;
    }
    public String buttonOp_action() {
	this.getSessionEncoladorBean().setRenderAlert(false);
	String ordenProceso_=(String)this.textFieldOp.getValue();
        java.util.List treeChildren = treeNodeFolder.getChildren();
	treeChildren.clear();
	treeNodeFolder.setText((String)this.textFieldOp.getValue());
	ConstantesGenerales.getInstance();

	File folder=new File(ConstantesGenerales.OP_FOLDER+"\\"+ordenProceso_);
	treeNodeFolder.setExpanded(false);	
	
	if(folder!=null && folder.isDirectory() && !ordenProceso_.trim().equals("")){
	    String[] arrayDir=folder.list();
	    if(arrayDir.length>0){
		for(int i=0;i<arrayDir.length;i++){
		    int nodeNum = treeNodeFolder.getChildCount() + 1;
		    String nameNode=arrayDir[i];
		   // Create a new tree node and set its 
		   // text, imageURL, and ID properties.
		   TreeNode newNode = new TreeNode();
		   newNode.setText(nameNode);	       
		   newNode.setId("newTreeNode" + nodeNum);
    //	       Checkbox box=new Checkbox();
		   checkBox = new Checkbox();
		   checkBox.setLabel(nameNode);
		   checkBox.setSelectedValue(nameNode);
		   checkBox.setName("CheckBoxFile");
		   newNode.getFacets().put(newNode.CONTENT_FACET_KEY, checkBox);
		   // Add the node to the list and select it in the tree.
		   treeChildren.add(newNode);
		}
		 this.getSessionEncoladorBean().setRenderTree(true);
	    }else{
		this.getSessionEncoladorBean().setRenderAlert(true);
		this.setMensajeAlert("warning", "Advertencia", EncoladorSource.getInstance().getEncoladorErrorSetupKey("CarpetaVacia"), true);	   
		this.getSessionEncoladorBean().setRenderTree(false);
	    }
	}else{
	    this.getSessionEncoladorBean().setRenderAlert(true);
	    this.setMensajeAlert("warning", "Advertencia", EncoladorSource.getInstance().getEncoladorErrorSetupKey("CarpetaNoExiste"), true);	   
	    this.getSessionEncoladorBean().setRenderTree(false);
	}      
	
        return null;
    }


    public String buttonProcesar_action() {
	this.getSessionEncoladorBean().setRenderAlert(false);
        List listCheck=this.checkBox.getSelected("CheckBoxFile");
	ArrayList fileList=new ArrayList();	
	if(listCheck!=null && listCheck.size()>0){
	    for(Iterator it=listCheck.iterator();it.hasNext();){
		String selectValue_=(String)it.next();		
		fileList.add(selectValue_);
	    }
	}
	
	int codigoImp_=Integer.parseInt((String)this.dropDownImpresora.getValue());
	int codigoJob_=Integer.parseInt((String)this.dropDownJob.getValue());
	boolean[][] matrizJobXImpresora=this.getSessionEncoladorBean().getMatrizJobxImpresora();
	
	
	if(matrizJobXImpresora[codigoJob_][codigoImp_]){
	    
            try {
                EncoladorDelegate encoladorDelegate = new EncoladorDelegate();
                encoladorDelegate.procesar();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            
	}else{
	    this.getSessionEncoladorBean().setRenderAlert(true);
	    this.setMensajeAlert("warning", "Advertencia",EncoladorSource.getInstance().getEncoladorErrorSetupKey("WarningJobNoPermitido"), true);	       
	}
	
        return null;
    }
    
    
    public void setMensajeAlert(String type, String summary, String detail, boolean visible) 
    {
        if (visible){
            this.alertMessage.setType(type);
            this.alertMessage.setSummary(summary);
            this.alertMessage.setDetail(detail);
            this.alertMessage.setVisible(visible);
        }else{
            this.alertMessage.setVisible(visible);
        }
    }

}
